package com.emar.adx.yiqifa.test;

import java.util.Stack;

/**
 * Created by xiaoliang on 2015/9/16 9:22.
 */
public class NumGamer {

    public static void main(String[] args) {
        NumGamer gamer = new NumGamer();
        gamer.test();
    }

    public void test(){

        int num = 15;
        int count = 0;

        for(int x0=0;x0<num;x0++){
            for(int x1 = 0; x1 <num; x1++){
                if(isFour(x0,"+",x1,"-",9)){

                    for(int y0=0;y0<num;y0++){
                        for(int y1=0;y1<num;y1++){
                            for(int y2=0;y2<num;y2++){
                                if(isFour(y0,"-",y1,"*",y2)){

                                    for(int z0=0;z0<num;z0++){
                                        for(int z1=0;z1<num;z1++){
                                            for(int z2=0;z2<num;z2++){
                                                if(isFour(z0,"+",z1,"-",z2)){

                                                    if(isFour(x0,"+",y0,"/",z0) && isFour(x1,"-",y1,"*",z1) && isFour(9,"-",y2,"-",z2)){
                                                        count++;
                                                        System.out.println(x0+" "+x1+" 3");
                                                        System.out.println(y0+" "+y1+" "+y2);
                                                        System.out.println(z0+" "+z1+" "+z2);
                                                        System.out.println(count+"============");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public boolean isFour(int x,String param1,int y,String param2,int z){

          Stack<Character> chars = new Stack<Character>();// 操作符栈
          Stack<Integer> nums = new Stack<Integer>();;// 操作数栈

        if("/".equals(param2) && z==0){
            return false;
        }

        if("*".equals(param2) || "/".equals(param2)){

            String s = numValue(y,param2,z);
            if(s.contains(".") || "error".equals(s)){
                return false;
            }
            int temp = Integer.valueOf(s);
            if("4".equals(numValue(x, param1, temp))){
                return true;
            }
        }else {
            String s = numValue(x,param1,y);
            if(s.contains(".") || "error".equals(s)){
                return false;
            }
            int temp = Integer.valueOf(s);
            if("4".equals(numValue(temp, param2,z))){
                return true;
            }
        }

        return false;
    }

    public String numValue(int x,String param1,int y){
        if("+".equals(param1)){
            return String.valueOf(x+y);
        }else if("-".equals(param1)){
            return String.valueOf(x-y);
        }else if("*".equals(param1)){
            return String.valueOf(x*y);
        }else {
            if(x%y!=0){
                return "error";
            }else {
                return String.valueOf(x/y);
            }
        }
    }

}
